[[...path]].page.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import React from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import Head from 'next/head';
  10. import CountBadge from '~/components/Common/CountBadge';
  11. import PageListIcon from '~/components/Icons/PageListIcon';
  12. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  13. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  14. import { Page } from '~/components/Page';
  15. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  16. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  17. import TableOfContents from '~/components/TableOfContents';
  18. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  19. import { CrowiRequest } from '~/interfaces/crowi-request';
  20. import { RendererConfig } from '~/interfaces/services/renderer';
  21. import { IShareLinkHasId } from '~/interfaces/share-link';
  22. import {
  23. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  24. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri,
  25. } from '~/stores/context';
  26. import { useDescendantsPageListModal } from '~/stores/modal';
  27. import loggerFactory from '~/utils/logger';
  28. import {
  29. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  33. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  34. type Props = CommonProps & {
  35. shareLink?: IShareLinkHasId,
  36. isExpired: boolean,
  37. disableLinkSharing: boolean,
  38. isSearchServiceConfigured: boolean,
  39. isSearchServiceReachable: boolean,
  40. isSearchScopeChildrenAsDefault: boolean,
  41. drawioUri: string | null,
  42. rendererConfig: RendererConfig,
  43. };
  44. const SharedPage: NextPage<Props> = (props: Props) => {
  45. useIsSearchPage(false);
  46. useShareLinkId(props.shareLink?._id);
  47. useCurrentPageId(props.shareLink?.relatedPage._id);
  48. useCurrentUser(props.currentUser);
  49. useCurrentPathname(props.currentPathname);
  50. useRendererConfig(props.rendererConfig);
  51. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  52. useIsSearchServiceReachable(props.isSearchServiceReachable);
  53. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  54. useDrawioUri(props.drawioUri);
  55. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  56. const { t } = useTranslation();
  57. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  58. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  59. const shareLink = props.shareLink;
  60. const title = generateCustomTitle(props, 'GROWI');
  61. return (
  62. <>
  63. <Head>
  64. <title>{title}</title>
  65. </Head>
  66. <DrawioViewerScript />
  67. <ShareLinkLayout expandContainer={props.isContainerFluid}>
  68. <div className="h-100 d-flex flex-column justify-content-between">
  69. <header className="py-0 position-relative">
  70. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  71. </header>
  72. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  73. <div className="flex-grow-1">
  74. <div id="content-main" className="content-main">
  75. <div className="grw-container-convertible">
  76. { props.disableLinkSharing && (
  77. <div className="mt-4">
  78. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  79. </div>
  80. )}
  81. { (isNotFound && !props.disableLinkSharing) && (
  82. <div className="container-lg">
  83. <h2 className="text-muted mt-4">
  84. <i className="icon-ban" aria-hidden="true" />
  85. <span> Page is not found</span>
  86. </h2>
  87. </div>
  88. )}
  89. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  90. <div className="container-lg">
  91. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  92. <h2 className="text-muted mt-4">
  93. <i className="icon-ban" aria-hidden="true" />
  94. <span> Page is expired</span>
  95. </h2>
  96. </div>
  97. )}
  98. {(isShowSharedPage && shareLink != null) && (
  99. <>
  100. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  101. <div className="d-flex flex-column flex-lg-row-reverse">
  102. <div className="grw-side-contents-container">
  103. <div className="grw-side-contents-sticky-container">
  104. {/* Page list */}
  105. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  106. { shareLink.relatedPage.path != null && (
  107. <button
  108. type="button"
  109. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  110. rounded-pill d-flex justify-content-between align-items-center"
  111. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  112. data-testid="pageListButton"
  113. >
  114. <div className="grw-page-accessories-control-icon">
  115. <PageListIcon />
  116. </div>
  117. {t('page_list')}
  118. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  119. </button>
  120. ) }
  121. </div>
  122. <div className="d-none d-lg-block">
  123. <TableOfContents />
  124. </div>
  125. </div>
  126. </div>
  127. <div className="flex-grow-1 flex-basis-0 mw-0">
  128. <Page />
  129. </div>
  130. </div>
  131. </>
  132. )}
  133. </div>
  134. </div>
  135. </div>
  136. </div>
  137. </ShareLinkLayout>
  138. </>
  139. );
  140. };
  141. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  142. const req: CrowiRequest = context.req as CrowiRequest;
  143. const { crowi } = req;
  144. const { configManager, searchService, xssService } = crowi;
  145. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  146. props.isSearchServiceConfigured = searchService.isConfigured;
  147. props.isSearchServiceReachable = searchService.isReachable;
  148. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  149. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  150. props.rendererConfig = {
  151. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  152. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  153. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  154. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  155. plantumlUri: process.env.PLANTUML_URI ?? null,
  156. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  157. // XSS Options
  158. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  159. attrWhiteList: xssService.getAttrWhiteList(),
  160. tagWhiteList: xssService.getTagWhiteList(),
  161. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  162. };
  163. }
  164. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  165. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  166. props._nextI18Next = nextI18NextConfig._nextI18Next;
  167. }
  168. function getAction(props: Props): SupportedActionType {
  169. let action: SupportedActionType;
  170. if (props.isExpired) {
  171. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  172. }
  173. else if (props.shareLink == null) {
  174. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  175. }
  176. else {
  177. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  178. }
  179. return action;
  180. }
  181. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  182. const req: CrowiRequest = context.req as CrowiRequest;
  183. const parameters = {
  184. ip: req.ip,
  185. endpoint: req.originalUrl,
  186. action,
  187. user: req.user?._id,
  188. snapshot: {
  189. username: req.user?.username,
  190. },
  191. };
  192. await req.crowi.activityService.createActivity(parameters);
  193. }
  194. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  195. const req = context.req as CrowiRequest<IUserHasId & any>;
  196. const { crowi, params } = req;
  197. const result = await getServerSideCommonProps(context);
  198. if (!('props' in result)) {
  199. throw new Error('invalid getSSP result');
  200. }
  201. const props: Props = result.props as Props;
  202. try {
  203. const ShareLinkModel = crowi.model('ShareLink');
  204. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  205. if (shareLink != null) {
  206. props.isExpired = shareLink.isExpired();
  207. props.shareLink = shareLink.toObject();
  208. }
  209. }
  210. catch (err) {
  211. logger.error(err);
  212. }
  213. injectServerConfigurations(context, props);
  214. await injectNextI18NextConfigurations(context, props);
  215. await addActivity(context, getAction(props));
  216. return {
  217. props,
  218. };
  219. };
  220. export default SharedPage;